package com.sneakxy.cloudbase.platform.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sneakxy.cloudbase.platform.bean.Account;
import com.sneakxy.cloudbase.platform.repository.AccountRepository;
import com.sneakxy.mybatis.commons.repository.query.domain.CriteriaBuilder;

/**
 * 
 * @author 潜行的青衣
 * @see
 */
@Service
public class AccountService extends BaseService<Account, String, AccountRepository> implements IAccountService {

	@Value("${merge.size.account}")
	private int mergeSize;
	
	@Autowired
	private IAccountRoleService accountRoleService;
	
	@Autowired
	private ILauncherSettingService launcherSettingService;
	
	/* (non-Javadoc)
	 * @see com.sneakxy.cloudbase.platform.service.IAccountService#findBySearch(java.lang.String, org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<Account> findBySearch(String search, Pageable pageable) {
		if(StringUtils.isBlank(search)) {
			return getRepository().findAll(pageable);
		}
		CriteriaBuilder<Account> builder = new CriteriaBuilder<>(Account.class);
		String value = StringUtils.join("%", StringUtils.trimToEmpty(search), "%");
		builder.orLike("displayName", value);
		builder.orLike("account", value);
		builder.orLike("memo", value);
		return getRepository().findAll(builder.build(), pageable);
	}
	
	@Override
	public Page<Account> findBySearch(String roleId, String search, Pageable pageable) {
		String value = StringUtils.join("%", StringUtils.trimToEmpty(search), "%");
		return getRepository().findByRoleId(roleId, value, pageable);
	}

	@Override
	public Account findOneByAccount(String account, String host) {
		CriteriaBuilder<Account> builder = new CriteriaBuilder<>(Account.class);
		builder.andEq("account", account);
		if(StringUtils.isNotBlank(host)) {
			builder.andEq("host", host);
		} else {
			builder.andIsNull("host", "(", StringUtils.EMPTY);
			builder.orEq("host", StringUtils.EMPTY, StringUtils.EMPTY, ")");
		}
		return findOne(builder.build());
	}

	@Override
	public boolean validate(String account, String host) {
		return findOneByAccount(account, host) == null;
	}

	@Override
	public boolean validate(String id, String account, String host) {
		CriteriaBuilder<Account> builder = new CriteriaBuilder<>(Account.class);
		builder.andEq("account", account);
		if(StringUtils.isNotBlank(host)) {
			builder.andEq("host", host);
		} else {
			builder.andIsNull("host", "(", StringUtils.EMPTY);
			builder.orEq("host", StringUtils.EMPTY, StringUtils.EMPTY, ")");
		}
		builder.andNotEq("id", id);
		return count(builder.build()) == 0;
	}

	@Override
	public Account findOne(String id) {
		return this.findOne(id, getTableName(id, getMergeSize()));
	}

	@Override
	public <S extends Account> S save(S entity) {
		return super.save(entity, getTableName(entity.getId(), getMergeSize()));
	}

	@Override
	public void delete(String id) {
		this.delete(id, getTableName(id, getMergeSize()));
	}

	public int getMergeSize() {
		return mergeSize;
	}

	@Override
	@Transactional
	public void removeCascad(String accountid) {
		//删除角色关联
		accountRoleService.deleteByAccountId(accountid);
		String tableName = launcherSettingService.getTableName(accountid);
		//删除设置关联
		launcherSettingService.deleteByAccountId(accountid, tableName);
		//删除账户
		delete(accountid);
	}

}
